package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bwie.utils.R;
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Menu;
import com.bwie.mapper.MenuMapper;
import com.bwie.service.MenuService;

import javax.servlet.http.HttpServletRequest;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MenuMapper menuMapper;
    @Override
    public R getTree() {
        String token = request.getHeader("token");
        Integer userId = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("userId", Integer.class);

        List<Menu> menuTree = menuMapper.getTree(userId);

        List<Tree<Integer>> treeList = TreeUtil.build(menuTree,0,(menu, TreeNode)->{
            TreeNode.setId(menu.getMenuId());
            TreeNode.setParentId(menu.getPid());
            TreeNode.setName(menu.getMenuName());
            TreeNode.putExtra("path",menu.getPath());
        });

        return R.OK(treeList);
    }
}
